$(document).ready(function(){
 var type = $("#admin_service_item").attr('type');
  var title = $(".title").text();
	$(".save_txt").click(function(){
	  var ID=$(this).attr('id');
	  var title =  $("#title_input_"+ID).val();
	  var description =  $("#des_input_"+ID).val();
	  var newImg = $("#newImg_"+ID).val();
	  var img = $("#curImg_"+ID).attr('src').substring(baseurl.length)
	  if (newImg) {
		  img = newImg;
	  }
		if (description != "") {
			  $.ajax({
			  url: baseurl + "admin_services/editItem/",
			  type: "post",
			  data:{'img':img,'description':description,'ID':ID,'title':title},
			  success: function(str){
				  var string = $.parseJSON(str);
					  if (string.success == false) {
						  alert(string.reason);
					  }else{
						  $("#title_"+ID).text(title);
						  $("#des_"+ID).text(description);
						  $("#curImg_"+ID).attr("src", baseurl + img);
						  $("#PhotoPrevs_"+ID).empty();
						  $("#"+ID+" .edit_txt").toggle();
						  $("#"+ID+" .save_txt").toggle();
						  $("#"+ID+" .cancel_txt").toggle();
						  $("#"+ID+" .value").toggle();
						  $("#"+ID+" .input").toggle();
					  }
			  },
				  error: function(str){alert('Error!! Please contact the administrator.');}
			  });
		}else{
			alert('Please enter description');
		}
	});
	
   $("#submit_new").click(function(){
	  var title =  $("#title").val();
	  var description =  $("#description").val();
	  var newImg = $("#newImg").val();
		if (description != "" && title !="") {
			  $.ajax({
			  url: baseurl + "admin_services/addItem/"+type,
			  type: "post",
			  data:{'title':title,'description':description, 'newImg':newImg},
			  success: function(str){
				  var string = $.parseJSON(str);
					  if (string.success == false) {
						  alert(string.reason);
					  }else{
						  location.reload();
					  }
			  },
				  error: function(str){alert('Error!! Please contact the administrator.');}
			  });
		}else{
			alert('Please enter both title and description');
		}
  });
  
  $(".delete_all_rows").click(function(){
	  var r=confirm("Are you to delete the records?");
	  if (r==true) {
		var ids = new Array();
		$("input[@name=row_sel]:checked" ).each(function() {
		  var id=$(this).attr('id');
		  ids.push(id);
		});
		$.ajax({
			url: baseurl + "admin_services/deleteItem",
			type: "post",
			data:{'ids':ids},
			//dataType: 'json',
			success: function(str){
				var string = $.parseJSON(str);
					if (string.success == false) {
						alert(string.reason);
					}else{
						//$("#des_"+ID).text(description);
						ids.forEach(function(entry) {
							$("tr#"+entry).empty();
						});
						alert('Sucessfully delete selected rows');
					}
			},
				error: function(str){alert('Error!! Please contact the administrator.');}
			});
	  } else {
	  
	  }
  });
	
  $('.add_new').magnificPopup({
  type: 'inline',
  midClick: true,
  closeBtnInside: true,
  closeOnBgClick: false,
  });
 
  $('#UploadImages').uberuploadcropper({
	//---------------------------------------------------
	// uploadify options..
	//---------------------------------------------------
	fineuploader: {
		//debug : true,
		request	: { 
			// params: {}
			endpoint: baseurl + 'upload/action' 
		},						
		validation: {
			sizeLimit	: 1024 * 1000, //in bytes
			allowedExtensions: ['jpg','jpeg','png','gif']
		}
	},
	//---------------------------------------------------
	//now the cropper options..
	//---------------------------------------------------
	jcrop: {
		aspectRatio  : 2, 
		allowSelect  : false, //can reselect
		allowResize  : true,  //can resize selection
		setSelect    : [ 0, 0, 200, 100 ], //these are the dimensions of the crop box x1,y1,x2,y2
		minSize      : [ 200, 100 ], //if you want to be able to resize, use these
		maxSize      : [ 380, 380 ]
	},
	//---------------------------------------------------
	//now the uber options..
	//---------------------------------------------------
	folder           : baseurl + 'resources/upload_imgs/', // only used in uber, not passed to server
	cropAction       : baseurl + 'crop/action', // server side request to crop image
	onComplete       : function(e,imgs,data){ 
		var $PhotoPrevs = $('#PhotoPrevs');
		for(var i=0,l=imgs.length; i<l; i++){
			$('.qq-upload-list').empty();
			$PhotoPrevs.html('<img width="280px" height="280px" src="'+baseurl+'resources/upload_imgs/'+ imgs[i].filename +'?d='+ (new Date()).getTime() +'" />');
			$PhotoPrevs.append('<input id="newImg'+'" type="hidden" name="newImg" value="resources/upload_imgs/'+ imgs[i].filename +'?d='+ (new Date()).getTime() +'" />');
		}
	}
  });
});

 
